package com.sjtuy.galtxtreader;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;

import androidx.documentfile.provider.DocumentFile;

import com.sjtuy.galtxtreader.bookfile.BookImportConverter;
import com.sjtuy.galtxtreader.bookfile.DllDialogSplitter;
import com.sjtuy.galtxtreader.bookfile.PunctuationsBasedDialogSplitter;
import com.sjtuy.galtxtreader.bookfile.SimpleDialogSplitter;
import com.sjtuy.galtxtreader.file.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import static com.sjtuy.galtxtreader.file.utils.copy;
import static com.sjtuy.galtxtreader.file.utils.deleteDir;
import static com.sjtuy.galtxtreader.file.utils.openFileInDir;

public class BookListAdapter extends BaseAdapter {
    Context context;
    ListView listView;
    File booksDir;
    public BookListAdapter(ListView listView){
        this.listView=listView;
        context=listView.getContext();
        booksDir=openFileInDir(context.getExternalFilesDir(""),"books");
        if(!booksDir.exists()){
            booksDir.mkdir();
        }
    }
    public void addBook(Uri bookUri){
        DocumentFile file = DocumentFile.fromSingleUri(context,bookUri);

        File tmpDir = openFileInDir(booksDir,file.getName().split(".txt")[0]);
        if(!tmpDir.exists()){
            tmpDir.mkdir();
            try{
                copy(context.getContentResolver().openInputStream(bookUri),
                        new FileOutputStream(tmpDir.getPath()+"/txt"));
                BookImportConverter bc = new BookImportConverter(tmpDir);
                bc.setDialogSplitter(new PunctuationsBasedDialogSplitter(50,60));
                bc.split();
                notifyDataSetChanged();
            }
            catch (Exception ex){
                ex.printStackTrace();
            }
        }
    }
    @Override
    public int getCount() {
        return booksDir.listFiles().length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final BookListItemView bookListItemView = new BookListItemView(context);
        bookListItemView.setBookDir(booksDir.listFiles()[position]);
        bookListItemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                new AlertDialog.Builder(context).setTitle("确定删除"+bookListItemView.getBookName()+"?")
                        .setIcon(R.drawable.ic_baseline_delete_forever_24)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                deleteDir(bookListItemView.getBookDir());
                                notifyDataSetChanged();
                            }
                        }).setNegativeButton("取消",null).create().show();
                return false;
            }
        });
        return bookListItemView;
    }
}
